package com.kehao.netease;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Q1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s = sc.nextLine().split(" ");
        Map<Integer, Integer> idToIndexMap = new HashMap<>();
        int[] aIds = new int[s.length];
        for (int i = 0; i < s.length; i++) {
            aIds[i] = Integer.parseInt(s[i]);
            idToIndexMap.put(aIds[i], i);
        }
        s = sc.nextLine().split(" ");
        int[] bIds = new int[s.length];
        for (int i = 0; i < s.length; i++) {
            bIds[i] = Integer.parseInt(s[i]);
            idToIndexMap.put(bIds[i], i);
        }
        int count = Integer.parseInt(sc.nextLine());
        int[][] relationship = new int[aIds.length][bIds.length];
        for (int i = 0; i < count; i++) {
            String[] s1 = sc.nextLine().split(" ");
            int id1 = Integer.parseInt(s1[0]);
            int id2 = Integer.parseInt(s1[1]);
            relationship[idToIndexMap.get(id1)][idToIndexMap.get(id2)] = 1;
        }
        int[][] data = init(relationship);
        int majorCount = 0;
        while (isNotEmpty(data)) {
            int[] maxIndex = getMaxIndex(data);
            if (maxIndex[0] == 0) {
                for (int j = 0; j < relationship[0].length; j++) {
                    relationship[maxIndex[1]][j] = 0;

                }
            } else {
                for (int i = 0; i < relationship.length; i++) {
                    relationship[i][maxIndex[1]] = 0;
                }
            }
            majorCount++;
            data = init(relationship);
        }
        System.out.println(majorCount);

    }

    private static int[] getMaxIndex(int[][] data) {
        int[] result = new int[2];
        int max = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                if (max < data[i][j]) {
                    result[0] = i;
                    result[1] = j;
                    max = data[i][j];
                }
            }
        }
        return result;
    }

    private static boolean isNotEmpty(int[][] data) {
        for (int[] datum : data) {
            for (int i : datum) {
                if (i == 1) {
                    return true;
                }
            }
        }
        return false;
    }

    private static int[][] init(int[][] relationship) {
        int[][] result = new int[2][];
        result[0] = new int[relationship.length];
        result[1] = new int[relationship[0].length];
        for (int i = 0; i < relationship.length; i++) {
            int count = 0;
            for (int j = 0; j < relationship[0].length; j++) {
                if (relationship[i][j] == 1) {
                    count++;
                }
            }
            result[0][i] = count;
        }

        for (int j = 0; j < relationship[0].length; j++) {
            int count = 0;
            for (int i = 0; i < relationship.length; i++) {
                if (relationship[i][j] == 1) {
                    count++;
                }
            }
            result[1][j] = count;
        }
        return result;
    }


}
